const AdminBiz = require('../../../../../../comm/biz/admin_biz.js');
const pageHelper = require('../../../../../../helper/page_helper.js');
const validate = require('../../../../../../helper/validate.js');
const AdminActivityBiz = require('../../../../biz/admin_activity_biz.js');
const projectSetting = require('../../../../public/project_setting.js');
const RequestBiz = require('../../../../../../comm/biz/request_biz.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		if (!AdminBiz.isAdmin(this)) return;
		if (!pageHelper.getOptions(this, options)) return;

		wx.setNavigationBarTitle({
			title: projectSetting.ACTIVITY_NAME + '-修改',
		});

		this._loadDetail();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () { },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
		await this._loadDetail();
		this.selectComponent("#cmpt-form").reload();
		wx.stopPullDownRefresh();
	},

	model: function (e) {
		pageHelper.model(this, e);
	},

	_loadDetail: async function () {
		if (!AdminBiz.isAdmin(this)) return;

		let id = this.data.id;
		if (!id) return;

		if (!this.data.isLoad)  {
      let data = await AdminActivityBiz.initFormData();
      this.setData(data);
    }

		let params = {
			id
		};
		let retData = await RequestBiz.request('/api/ysjl/activity/activityInfo', params);
		let activity = retData.data.data;
		if (!activity) {
			this.setData({
				isLoad: null
			})
			return;
    };
		this.setData({
			isLoad: true,
			id: activity.id,
			formTitle: activity.title,
			formCateId: activity.cateId,

			formMaxCnt: activity.maxCnt,
			formStart: activity.startTime,
			formEnd: activity.endTime,
			formStop: activity.stopTime,

			formAddress: activity.address,
			formAddressGeo: {
				address: '',
				latitude: activity.addressLat,
				longitude: activity.addressLon,
				name: ''
			},
		});

	},

	bindFormSubmit: async function () {
		if (!AdminBiz.isAdmin(this)) return;

		// 数据校验
		let data = this.data;
		data = validate.check(data, AdminActivityBiz.CHECK_FORM, this);
		if (!data) return;

		if (data.endTime < data.startTime) {
			return pageHelper.showModal('结束时间不能早于开始时间');
		}
		if (data.endTime < data.stopTime) {
			return pageHelper.showModal('结束时间不能早于停止报名时间');
		}
		if (data.stopTime > data.startTime) {
			return pageHelper.showModal('停止报名时间不能晚于开始时间');
    }

		let req = RequestBiz.request('/api/ysjl/activity/update', {
			id: this.data.id,
			title: data.title,
			cateId: data.cateId,
			maxCnt: data.maxCnt,
			startTime: data.startTime + ":00",
			endTime: data.endTime + ":00",
			stopTime: data.stopTime + ":00",
			address: data.address,
    });
    
    if (data.addressGeo) {
      this.setData({
        addressLat: data.addressGeo.latitude,
        addressLon: data.addressGeo.longitude
      })
    }

		req.then(res => {
			let retData = res.data;
      let callback = async function () {
        wx.navigateBack();
      }
			if (retData.ret == 0) {
				pageHelper.showSuccToast('修改成功', 2000, callback);
      }
      else {
				pageHelper.showErrToast(retData.msg, 2000, callback);
      }
		});

	},

	bindMapTap: function (e) {
		AdminActivityBiz.selectLocation(this);
	},

	url: function (e) {
		pageHelper.url(e, this);
	},

	switchModel: function (e) {
		pageHelper.switchModel(this, e);
	},

	bindSelectCmpt: function (e) {
		this.setData({
			formCateId: e.detail
		});
	},

})